package com.pfzy.utildemo.activity.record.sdk.pcm;

import com.pfzy.lib.config.log.MyLog;
import com.pfzy.utildemo.activity.record.soundrecord.RecordingInfo;
import com.pfzy.utildemo.activity.record.sdk.pcm.aac.AACRecordHelper;
import com.pfzy.utildemo.activity.record.sdk.pcm.mp3.DataEncodeThread;
import com.pfzy.utildemo.activity.record.sdk.pcm.mp3.MP3Recorder;

import java.io.IOException;

/**
 * 录音助手
 */
public class RecordHelper {
    private static RecordHelper mInstance;
    private RecordingInfo mRecordingInfo; // 正在录音
    private Boolean isAAC;

    public static RecordHelper getInstance() {
        if (mInstance == null) {
            mInstance = new RecordHelper();
        }
        return mInstance;
    }

    RecordHelper() {}

    public void startRecord(String path, RecordingInfo mRecrodInfo) {
        if (isRecording()) {
            _logd("当前正在录音");
            return;
        }

        this.mRecordingInfo = mRecrodInfo;

        if (isAAC) {
            AACRecordHelper.getInstance().start(path);
        }
        else{
            try {
                MP3Recorder.getInstance().start(path, new DataEncodeThread.EncodeThreadCallBack() {
                    @Override
                    public void onFlushAndRelease() {
                        _logd(" 录音文件转码完成 onFlushAndRelease");
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void stopRecord() {
        this.mRecordingInfo = null;
        if (isAAC) {
            AACRecordHelper.getInstance().stop();
        } else {
            MP3Recorder.getInstance().stop();
        }
    }

    /**
     * 更改新记录路径
     * @param path
     */
    public void changeNewRecordPath(String path) {
        if (isAAC) {
            AACRecordHelper.getInstance().changeNewRecordPath(path);
        } else {
            MP3Recorder.getInstance().changeToNewRecordPath(path);
        }
    }

    public boolean isRecording() {
        return isAAC ? AACRecordHelper.getInstance().isRecording() : MP3Recorder.getInstance().isRecording();
    }

    public RecordingInfo getCurrentRecordInfo() {
        if (!isRecording()) {
            return null;
        }
        return this.mRecordingInfo;
    }

    private void _logd(String msg) {
        MyLog.logd("录音助手 " + msg);
    }
}